package com.me.learnspring.controller;

import com.me.learnspring.controller.request.NewOrderRequest;
import com.me.learnspring.model.Drink;
import com.me.learnspring.model.Goods;
import com.me.learnspring.model.KVPair;
import com.me.learnspring.model.Order;
import com.me.learnspring.repository.DrinkRepository;
import com.me.learnspring.repository.OrderRepository;
import com.me.learnspring.service.OrderService;
import lombok.var;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.reflect.Array;
import java.time.LocalDateTime;
import java.util.*;

@Controller
@RequestMapping("/order")
public class OrderController {
	@Autowired
	private OrderService orderService;
	
	@PostMapping("/")
	public Order saveOrder(@RequestBody NewOrderRequest o) {
		return orderService.saveOrder(o);
	}
	
	@GetMapping("/{customer}")
	public List<Order> getOrderByCustomer(@PathVariable String customer) {
		return orderService.findByCustomer(customer);
	}
	
	@PostMapping("/cancel")
	public String cancleOrder(String id,String origin, HttpServletRequest req) {
		orderService.cancle(Long.valueOf(id));
		return "redirect:" + origin;
	}
	@PostMapping("/active")
	public String activeOrder(String id,String origin, HttpServletRequest req) {
		orderService.active(Long.valueOf(id));
		return "redirect:" + origin;
	}
	
	@PostMapping("/checkout")
	public ModelAndView checkout(NewOrderRequest oreq, HttpSession session, Model model) {
		var cart = (Map<Long, Goods>) session.getAttribute("cart");
		
		var orderGoods = new HashMap<Long, Goods>();
		int arraySize = 0;
		for (Long did : oreq.getDrinks()) {
			if (cart.containsKey(did)) {
				orderGoods.put(did,cart.remove(did));
				arraySize += orderGoods.get(did).getCount();
			}
		}
		
		Long[] ids = new Long[arraySize];
		int i = 0;
		for (Long key : orderGoods.keySet()) {
			for (int j = 0; j < orderGoods.get(key).getCount(); j++) {
				ids[i++] = key;
			}
		}
		oreq.setDrinks(ids);
		
		Order order = orderService.saveOrder(oreq);
		
		model.addAttribute("order", order);
		model.addAttribute("goodsMap", orderGoods);
		
		return new ModelAndView("/order/complete");
	}
	
	@GetMapping("")
	public String index(String username,Model model){
		List<Order> orders = orderService.findByCustomer(username);
		
		var list = new ArrayList<KVPair>();
		for (Order order : orders) {
			var goodsMap = new HashMap<Long, Goods>();
			for (Drink d : order.getItems()) {
				if (goodsMap.containsKey(d.getId())) {
					goodsMap.get(d.getId()).increase();
				} else {
					goodsMap.put(d.getId(), new Goods(1, d));
				}
			}
			list.add(new KVPair(order, goodsMap));
		}
		
		
		model.addAttribute("list", list);
		return "/order/index";
	}
	
	@GetMapping("/find")
	public String find(String startDate, String endDate,String username,Model model){
		List<Order> orders = orderService.find(startDate, endDate,username);
		
		var list = new ArrayList<KVPair>();
		for (Order order : orders) {
			var goodsMap = new HashMap<Long, Goods>();
			for (Drink d : order.getItems()) {
				if (goodsMap.containsKey(d.getId())) {
					goodsMap.get(d.getId()).increase();
				} else {
					goodsMap.put(d.getId(), new Goods(1, d));
				}
			}
			list.add(new KVPair(order, goodsMap));
		}
		model.addAttribute("list", list);
		return "/order/index";
	}
}
